package com.sugon.controller.device;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.sugon.entity.AlarmCodeBaseEntity;
import com.sugon.modules.alarm.service.AlarmCodeBaseService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 报警报警库Controller
 *
 * @author YuXD
 * @date 2023-03-16
 */
@Controller
@RequestMapping("alarmcodebase")
public class AlarmCodeBaseController {
    @Autowired
    private AlarmCodeBaseService alarmCodeBaseService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<AlarmCodeBaseEntity> alarmCategoryList = alarmCodeBaseService.queryList(query);
        int total = alarmCodeBaseService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(alarmCategoryList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看列表
     */
    @GetMapping("/query")
    @ResponseBody
    public R list(@RequestParam String devId, @RequestParam String alarmCode) {
        Map<String, Object> param = MapUtil.newHashMap();
        param.put("devId", devId);
        param.put("alarmCode", alarmCode);
        List<AlarmCodeBaseEntity> alarmCategoryList = alarmCodeBaseService.queryList(param);
        return R.ok().put("list", Optional.ofNullable(alarmCategoryList).orElse(CollectionUtil.newArrayList()));
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<AlarmCodeBaseEntity> list = alarmCodeBaseService.queryList(params);

        return R.ok().put("list", list);
    }
}
